package net.model.observer;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class ConcreteSubject implements ISubject {

	private List<IObserver> observers = new CopyOnWriteArrayList<IObserver>();
	private int param;
	
	@Override
	public void registerListener(IObserver observer) {
		this.observers.add(observer);
	}
	
	@Override
	public void unregisterListener(IObserver observer) {
		int n = this.observers.indexOf(observer);
		if (n>=0)
			this.observers.remove(n);
	}
	
	@Override
	public void notifyObservers() {
		for (int i=0; i<this.observers.size(); ++i) {
			IObserver observer = (IObserver)this.observers.get(i);
			observer.update(param);
		}
	}

	public int getParam() {
		return param;
	}

	@Override
	public void setParam(int param) {
		this.param = param;
		notifyObservers();
	}
}
